#include <memory>
#include <iostream>

class Context; // 前向声明

// 状态接口
class State{
public:
    virtual ~State() = default;
    virtual void handle(Context* context) = 0;
};

// 具体状态A
class ConcreteStateA : public State{
public:
    void handle(Context* context) override;
};

// 具体状态A
class ConcreteStateB : public State{
public:
    void handle(Context* context) override;
};

// 上下文类
class Context{
    std::unique_ptr<State> state;
public:
    Context() : state(std::make_unique<ConcreteStateA>()){}

    void setState(std::unique_ptr<State> s){ state = std::move(s); }

    void request(){
        state->handle(this);
    }
};

// 状态实现
void ConcreteStateA::handle(Context* context){
    // 处理逻辑，可能转换状态
    context->setState(std::make_unique<ConcreteStateB>());
}

void ConcreteStateB::handle(Context* context){
    // 处理逻辑，可能转换状态
    context->setState(std::make_unique<ConcreteStateA>());
}

